﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * 
 * */

using System;
using System.Collections.Generic;
using Origami.Data;
using Origami.Data.Provider;
using Origami.Data.Query;
using QWEntity.Local;

namespace QWRepository.Local
{
    /// <summary>
    /// UserBase : DataAccess, IDataAccess
    /// </summary>
    public class UserBase : DataAccess, IDataAccess<User>, IDisposable
    {
        /// <summary>
        /// _disposedValue
        /// </summary>
        private bool _disposedValue;

        private IEntityManager _em;

        /// <summary>
        /// Initializes a new instance of the <see cref="UserBase"/> class.
        /// </summary>
        public UserBase()
        {
            try
            {
                DataSource ds = GetDataSource("LOCAL");
                _em = new EntityManager(ds);
            }
            catch (NullReferenceException ex)
            {
                throw new Exception(string.Format("Unable to find Database Connection configuration files!\r\n{0}",
                                                  ex.Message));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        #region IDataAccess<User> Members

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public User GetById(string id)
        {
            return _em.Get<User>(id);
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns></returns>
        public List<User> GetAll()
        {
            return _em.Get<User>();
        }

        /// <summary>
        /// Gets the meta data.
        /// </summary>
        /// <returns></returns>
        public List<MetaData> GetMetaData()
        {
            return _em.GetMetaData<User>();
        }

        /// <summary>
        /// Creates the query.
        /// </summary>
        /// <returns></returns>
        public ObjectQuery CreateQuery()
        {
            return _em.CreateQuery<User>();
        }

        /// <summary>
        /// Saves the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public int Save(User obj)
        {
            return _em.Save(obj);
        }

        /// <summary>
        /// Updates the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public int Update(User obj)
        {
            return _em.Update(obj);
        }

        /// <summary>
        /// Deletes the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public int Delete(User obj)
        {
            return _em.Delete(obj);
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public virtual void Dispose()
        {
            if (!_disposedValue)
            {
                try
                {
                    _em = null;
                }
                finally
                {
                    _disposedValue = true;
                    GC.SuppressFinalize(this);
                }
            }
        }

        #endregion

        /// <summary>
        /// Authenticates the user.
        /// </summary>
        /// <param name="usr">The usr.</param>
        /// <param name="pwd">The PWD.</param>
        /// <returns></returns>
        public bool AuthenticateUser(string usr, string pwd)
        {
            ObjectQuery q = _em.CreateQuery<User>();
            q.AddCriteria(Criteria.Equal("UserId", usr));
            q.AddCriteria(Criteria.Equal("Password", pwd));
            return q.List<User>().Count > 0;
        }
    }
}